package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Dic;
import cn.wolfcode.crm.domain.Dicitem;
import cn.wolfcode.crm.query.DicitemQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDicService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("dic")
public class DicController {

    @Autowired(required = false)
    private IDicService dicService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"dic:view","字典页面"},logical = Logical.OR)
    public String view() throws Exception{
        return "dic/dic";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"dic:saveOrUpdate","字典添加/编辑"},logical = Logical.OR)
    public JsonResult saveOrUpdate(Dic entity) throws Exception{
        JsonResult jsonResult = new JsonResult();
        if(entity.getId()==null){
            dicService.insert(entity);
        }else{
            dicService.updateByPrimaryKey(entity);
        }
        return jsonResult;
    }


    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo) throws Exception{
        return dicService.query(qo);
    }


    @RequestMapping("listitem")
    @ResponseBody
    public PageResult list(DicitemQueryObject qo) throws Exception{
        return dicService.queryitem(qo);
    }

    @RequestMapping("saveDic")
    @ResponseBody
    public JsonResult saveDic(Dic entity){
        JsonResult result = new JsonResult();
        if(entity.getId()!=null){
            dicService.updateByPrimaryKey(entity);
        }else{
            dicService.insert(entity);
        }

        return result;
    }
    @RequestMapping("saveDicitem")
    @ResponseBody
    public JsonResult saveDicitem(Dicitem entity){
        JsonResult result = new JsonResult();
        if(entity.getId()!=null){
            dicService.updateByPrimaryKey(entity);
        }else{
            dicService.insert(entity);
        }
        return result;
    }

    @RequestMapping("change")
    @ResponseBody
    public JsonResult change(Long id){
        JsonResult result = new JsonResult();
        dicService.changeByPrimaryKey(id);
        return result;
    }
    @RequestMapping("changeitem")
    @ResponseBody
    public JsonResult changeitem(Long id){
        JsonResult result = new JsonResult();
        dicService.changebyitemid(id);
        return result;
    }

    @RequestMapping("selectItemBySn")
    @ResponseBody
    public List<Dicitem> selectItemBySn(String sn){
        List<Dicitem> dicitems = dicService.selectItemBySn(sn);
        Dicitem d=new Dicitem();
        d.setId(-1L);
        d.setName("---所有---");
        dicitems.add(d);
        return dicitems;
    }



}
